<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\Menu;

use Illuminate\Support\Facades\Cache;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //为什么没有使用l5-repository;
        //1,因为数据量不大
        //2,因为时间紧促
        //查询菜单数据;导入数据库时，需要自定义sort，这样就实现了顶级菜单的排序
            $datas = Menu::orderBy('sort', 'asc')->get()->toArray();

            foreach($datas as $key=>$v) {
                //判断是否是顶级菜单,如果是顶级菜单则查出他下面对应的所有子菜单
                if ($v['pid'] == 0) {
                    $menus[$key] = $v;
                    $menus[$key]['child'] = Menu::where('pid', $v['id'])->get();

                }

            }

        return view('home',compact('menus'));
    }
}




